import * as actionTypes from './constants'

const defaultstate = {
    selectGoods:[],//购物中数据初始状态
    isAllSelect:false,//是否全选
    isDelete:false//是否开启删除状态
}
const reducer = (state = defaultstate,action) =>{
    console.log('启动了reducer');
    // console.log(action.TY);
    switch(action.type){
        case actionTypes.CHANGE_SELECTGOODS:
            //深拷贝 先转成JSON字符串 ， 在转成对象 ,在最后加一个isAllSelect 是因为购物车里添加了新的种类，全选状态肯定会取消    
            return {...state,selectGoods: JSON.parse(JSON.stringify([...state.selectGoods, action.data])),isAllSelect:false}

        case actionTypes.CHANGE_SELECTGOODS_COUNT:
                let newGoods = state.selectGoods;
                newGoods[action.index].count++;
                return {...state,selectGoods:JSON.parse(JSON.stringify(newGoods))}


        case actionTypes.SETADD_COUNT:
                let addGoodsCount = state.selectGoods;
                addGoodsCount[action.index].count++;
                return {...state,selectGoods:JSON.parse(JSON.stringify(addGoodsCount))}

        case actionTypes.SUBADD_COUNT:
                let subGoodsCount = state.selectGoods;
                subGoodsCount[action.index].count--;
                return {...state,selectGoods:JSON.parse(JSON.stringify(subGoodsCount))}

        case actionTypes.CHANGE_SELECT:
                let setSelect = state.selectGoods;
                setSelect[action.index].isSelect = !setSelect[action.index].isSelect
                //每次点击事件，都会判断一次，所有按钮的状态，若所有按钮的都为点击状态，则全选也为true
                if(state.selectGoods.findIndex((item)=> item.isSelect == false) == -1){
                    //-1 是没有找到 ，说明所的按钮 都为true
                    // console.log('11111111');
                    state.isAllSelect = true
                }else{

                    state.isAllSelect = false
                }
                return {...state,selectGoods:JSON.parse(JSON.stringify(setSelect)),isAllSelect:state.isAllSelect}
            //全选状态
        case actionTypes.ALL_SELECT:
                //设置新的变量时为了 深拷贝
                let newSelect = state.selectGoods;
                //当全选状态为true时，切所有isSelect的状态为true时候
                if(state.isAllSelect && state.selectGoods.findIndex((item)=> item.isSelect == false) == -1){
                    state.selectGoods.map((item)=>{
                        item.isSelect = false;
                    })
                }else{
                    state.selectGoods.map((item)=>{
                        //当按钮的状态为 false 才改变 
                      if(item.isSelect == false){
                          item.isSelect = !item.isSelect;
                      }  
                    })
                }
                return {...state, selectGoods: JSON.parse(JSON.stringify(newSelect)), isAllSelect: !state.isAllSelect}
        
        case actionTypes.DELETE_STATE:
                let  newDelete = state.selectGoods;
                state.selectGoods.map((item)=>{
                    item.isSelect = false
                })
                return {...state,selectGoods: JSON.parse(JSON.stringify(newDelete)),isAllSelect:false,isDelete:!state.isDelete}

        case actionTypes.DELETE_GOODS:
                // console.log('////////');
                let arr = []
                //把没有勾选中的 商品放到一个 数组里面，重新合并
                state.selectGoods.map((item,index)=>{
                    if(item.isSelect == false){
                        arr.push(state.selectGoods[index])
                    }
                })
                //进行删除后，全选状态一定为 false，直接传过去就行
                state.isAllSelect = false 
                state.isDelete = false
                return {...state,selectGoods: JSON.parse(JSON.stringify(arr)),isAllSelect: state.isAllSelect,isDelete:state.isDelete}
        case actionTypes.SETTLEMENT:
                //结算
                console.log(state.selectGoods,'/////');
                if(state.selectGoods.length !== 0){
                    let temp = confirm('您购物车里的货物一共'+action.data+"请问您确认购买吗？");
                    if(temp){
                        return  {...state,selectGoods: []}
                    }else{
                        return state
                    }
                    }
                else{
                    alert('您的购物车内还未添加货物')
                }
        default:
            console.log('default');
            return state
    }
}

export default reducer